<%@ page contentType="text/html" pageEncoding="UTF-8" %>
 <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ page import="com.liferay.portal.theme.ThemeDisplay" %>
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<%@ page import="com.liferay.portal.kernel.portlet.LiferayWindowState" %>

<portlet:defineObjects />
<liferay-theme:defineObjects/>
 <portlet:renderURL var="toAdd"  windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
	<portlet:param name="action" value="toAdd"/>
	<portlet:param name="redirect" value="<%= PortalUtil.getCurrentURL(request) %>" /> 
</portlet:renderURL> 
 <portlet:renderURL var="toEdit"  windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
	<portlet:param name="action" value="toEdit"/>
	<portlet:param name="redirect" value="<%= PortalUtil.getCurrentURL(request) %>" /> 
</portlet:renderURL> 
<portlet:resourceURL var='listSysDict' id='listSysDict' />
<portlet:resourceURL var='deleteSysDict' id='deleteSysDict' />
<div class="layui-elem-quote" style="margin-top:5px;">
		  <button class="layui-btn layui-btn-small" onclick="popupAddWin()"><i class="layui-icon">&#xe654;</i>增加</button> 
		  <button class="layui-btn layui-btn-small layui-btn-warm" onclick="popupEditWin()"> <i class="layui-icon">&#xe642;</i>编辑</button>
		  <button class="layui-btn layui-btn-small layui-btn-danger" onclick="deleteSysDict()"><i class="layui-icon">&#xe640;</i>删除</button>
		  <button class="layui-btn layui-btn-small" onclick="refreshPage()"><i class="layui-icon">&#x1002;</i>刷新</button>
		  
		<div class="layui-btn-group"  style="float: right">
		<label class="layui-form-label" style="height: 30px;">名称:</label>
		<div class="layui-input-inline">
			<input value="" name="searchName"   class="layui-input" type="text" style="height: 30px;" id="searchName">
		</div>
		<button class="layui-btn layui-btn-small"  onclick="initDataPage()" style="margin-top: -11px;"><i class="layui-icon">&#xe615;</i>查询</button>
       </div>
 </div>
    
 
<div class="layui-form">
  <table class="layui-table">
    <thead>
      <tr>
        <th width="50"><input name="categoryCheck" lay-skin="primary" lay-filter="allChoose" type="checkbox"></th>
        <th>序号</th>
        <th>类型</th>
        <th>字典名称</th>
        <th>关键字</th>
        <th>状态</th>
        <th>编号</th>
        <th>操作</th>
      </tr> 
    </thead>
    <tbody id="categoryTable"></tbody>
   </table>
   <div id="demo7"></div>
   <input name="currentPage" id = "currentPage" type="hidden">
</div>
 <!--模板-->
<script type="text/html" id="conTemp">
  {{# layui.each(d.records, function(index, item){ }}
    <tr>
        <tr><td><input name="categoryCheck" lay-skin="primary" type="checkbox" value="{{ item.id }}"></td>
        <td>{{index+1 }}</td>
        <td>{{ item.type }}</td>
        <td>{{ item.codeText }}</td>
        <td>{{ item.code }}</td>
         <td>
			{{# if (item.enable == 0) { }}
				<span class="label label-success">有效</span>
			{{# } else { }}
				<span class="label label-danger">无效</span>					
			{{# } }}
		</td>
	<td>{{ item.sortNo }}</td>
      </tr>
    {{# }); }}
</script> 
<script>
$(function (){
	initDataPage();
 }); 
layui.use('form', function(){
	  var $ = layui.jquery, form = layui.form();
	  //全选
	  form.on('checkbox(allChoose)', function(data){
	    var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
	    child.each(function(index, item){
	      item.checked = data.elem.checked;
	    });
	    form.render('checkbox');
	  });
s	});
 
function popupAddWin(){
	layer.open({
		  title: '<i class="layui-icon"></i>添加字典数据',
		  type: 2,
 		  fixed: false, //不固定
		  maxmin: true,
		  skin: 'layui-layer-molv',
		  area: ['540px', '385px'],
		  content:  ['${toAdd}', 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		}); 
}
function popupEditWin(){
	var chk_value =[];
	$('input[name="categoryCheck"]:checked').each(function(){
	chk_value.push($(this).val());
	});
  	if( chk_value.length==0 || chk_value.length>1  || chk_value == 'on' ){
		layer.alert('请选择一条需要编辑的记录!', {
			  icon: 0,
			  skin: 'layui-layer-molv' //样式类名
			});
		return;
	}
  	editRecode(chk_value);
}
function editRecode(id){
	layer.open({
		  title: '<i class="fa fa-windows"></i>编辑字典数据',
		  type: 2,
		  fixed: false, //不固定
		  maxmin: true,
		  skin: 'layui-layer-molv',
		  area: ['540px', '385px'],
		  content:  ['${toEdit}&id='+id, 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		}); 
}
function initDataPage(curr) {
    var pageSize = 10;
    //以下将以jquery.ajax为例，演示一个异步分页
    $.getJSON('${listSysDict}', {
    	current: curr || 1,
    	pages: pageSize,
        searchName : $("#searchName").val(),
        selectPage: curr || 1
    },
    function (res) { //从第1页开始请求。返回的json格式可以任意定义
    	 layui.use(['laypage', 'layer'], function(){
      	   var laypage = layui.laypage
      	   ,layer = layui.layer;
      	 laypage({
             cont: 'demo7', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
             pages: res.pages, //通过后台拿到的总页数
            skip: true ,
            curr: curr || 1,
             jump: function (obj,first) { //触发分页后的回调
                   if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                       initDataPage(obj.curr);
                  }
             }
         });
      	 });
        layui.use('laytpl', function(){
      	  var laytpl = layui.laytpl;
      	  var gettpl = document.getElementById('conTemp').innerHTML;   //获取拼接部分的内容  
            laytpl(gettpl).render(res, function (html) {       //給拼接的模板绑定数据  
                document.getElementById('categoryTable').innerHTML = html;    // 吧生成的结构绑定在负责呈现内容的div中。  
            }); 
      	}); 
        layui.use('form', function(){
        	var form = layui.form();
        	form.render('checkbox');
        	});
    });
}
function deleteSysDict(){
		var chk_value =[];
		$('input[name="categoryCheck"]:checked').each(function(){
		chk_value.push($(this).val());
		});
	  	if( chk_value.length==0  || chk_value == 'on' ){
			layer.alert('请选择需要删除的记录!', {
				  icon: 0,
				  skin: 'layui-layer-molv' //样式类名
				});
			return;
		}
	  	deleteRecode(chk_value);
	}
function deleteRecode(id){
		//询问框
		layer.confirm('确定要删除吗？', {
			icon: 3,
			skin: 'layui-layer-molv',
		  btn: ['确定','取消'] //按钮
		}, function(){
			$.ajax({
				   type: "post",
					url : "${deleteContent}&ids="+id,
					dataType : 'text',
					success : function(response){
						if(response=="success"){
							 layer.alert("删除记录成功!", {
								 icon: 1,
		          		  skin: 'layui-layer-molv' //样式类名
		          		  ,closeBtn: 0
		          		}, function(index1){
		          			layer.close(index1);
		          			initDataPage();

		          		});
		           }else{
		          	  layer.alert("删除出错!", {
		          		 icon: 2,
								    skin: 'layui-layer-molv'
								    ,closeBtn: 0
								  });
		           }
					},error  : function(response){
						 
					}
				  });
		} );
		  
	}
function refreshPage()
{
   window.location.reload();
}
</script>